home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / xhist / xhist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  22.6 KB  |  924 lines

  1.  
  2. /*
  3.  * xhist.c                              Brian Tierney,  12/89
  4.  *                    Felix Huang,  6/27/91
  5.  *
  6.  * This is an X window's based program to display a histogram
  7.  *   of a buffer of values.  The Buffer can be byte, short, or int
  8.  *   and of any arbitrary size.
  9.  */
  10.  
  11. #include "xhist.h"
  12.  
  13.  /* global window stuff */
  14. static xhist_win_objects *hist_win;
  15. Attr_attribute INSTANCE;
  16.  
  17. static Display *dpy;
  18. static Display *display;
  19. static XID xid;
  20. static GC gc;
  21. u_long    color_Red;
  22. u_long    color_vRed;
  23. u_long    color_Blue;
  24. u_long    color_Green;
  25. u_long    color_Orange;
  26.  
  27. Xv_window cpw;            /* global canvas paint window id */
  28.  
  29. /******************************************************/
  30. void
  31. main(argc, argv)
  32.     int       argc;
  33.     char    **argv;
  34. {
  35.     cur_fname = NULL;
  36.  
  37.     Progname = strsave(*argv);
  38.     hipserrlev = HEL_SEVERE;  /* only exit if severe errors */
  39.     hipserrprt = HEL_ERROR;  /* print messages for hips errors */
  40.  
  41.     if (argc > 1) {
  42.     cur_fname = (char *) malloc(100);
  43.     strcpy(cur_fname, argv[1]);
  44.     load_image();
  45.     } else if (isatty(0) == 0) {/* input redirect,  <  or ... |   */
  46.     cur_fname = "<stdin>";
  47.     load_image();
  48.     }
  49.     /*
  50.      * Initialize XView.
  51.      */
  52.     xv_init(XV_INIT_ARGS, argc, argv, 0);
  53.     INSTANCE = xv_unique_key();
  54.  
  55.     dpy = XOpenDisplay(NULL);
  56.  
  57.     /*
  58.      * Initialize user interface components.
  59.      */
  60.     hist_win = xhist_win_objects_initialize(NULL, NULL);
  61.  
  62.     /* printf("in main, after set: hist_win\n"); */
  63.  
  64.     cpw = (Xv_window) xv_get(hist_win->canvas, CANVAS_NTH_PAINT_WINDOW, NULL);
  65.     make_simple_colormap(cpw);
  66.  
  67.     /* printf("in main, after  make_simple_colormap(cpw)\n"); */
  68.  
  69.     if (argc > 1)
  70.     (void) xv_set(hist_win->file_item, PANEL_VALUE, argv[1], NULL);
  71.  
  72.     if (size != 0)
  73.     eval_proc();        /* image exists.     */
  74.  
  75.     /*
  76.      * Turn control over to XView.
  77.      */
  78.     window_main_loop(hist_win->win);
  79.     exit(0);
  80. }
  81.  
  82.  /**************************************************************************/
  83.  
  84. Xv_Font   font_small;
  85. Xv_Font   font_bold;
  86.  
  87. make_simple_colormap(xvwin)
  88.     Xv_window xvwin;
  89. {
  90.     Colormap  colmap;
  91.     XColor    mycolor;
  92.     XGCValues gc_val;
  93.     int       screen;
  94.  
  95.     display = (Display *) xv_get(xvwin, XV_DISPLAY, NULL);
  96. /*    XSetWindowBorderWidth(display, hist_win->canvas, 0); canvas */
  97.  
  98.     screen = XDefaultScreen(display);
  99.     colmap = XDefaultColormap(display, screen);
  100.  
  101.     font_bold = (Xv_Font) xv_find(xvwin, FONT,
  102.                   FONT_FAMILY, FONT_FAMILY_COUR,
  103.                   FONT_STYLE, FONT_STYLE_BOLD,
  104.                   NULL);
  105.  
  106.     font_small = (Xv_Font) xv_find(xvwin, FONT,
  107.                    FONT_FAMILY, FONT_FAMILY_ROMAN,
  108.                    NULL);
  109.  
  110.     if (XParseColor(display, colmap, "red", &mycolor, &mycolor) < 1) {
  111.     fprintf(stderr, "Error parsing color %s:", "red");
  112.     perror((char *) NULL);
  113.     exit(0);
  114.     }
  115.     mycolor.flags = DoRed | DoGreen | DoBlue;
  116.     if (XAllocColor(display, colmap, &mycolor) < 1) {
  117.     fprintf(stderr, "Error allocating color ");
  118.     perror((char *) NULL);
  119.     }
  120.     XInstallColormap(display, colmap);
  121.     color_Red = mycolor.pixel;
  122.  
  123.     if (XParseColor(display, colmap, "blue", &mycolor, &mycolor) < 1) {
  124.     fprintf(stderr, "Error parsing color %s:", "red");
  125.     perror((char *) NULL);
  126.     exit(0);
  127.     }
  128.     mycolor.flags = DoRed | DoGreen | DoBlue;
  129.     if (XAllocColor(display, colmap, &mycolor) < 1) {
  130.     fprintf(stderr, "Error allocating color ");
  131.     perror((char *) NULL);
  132.     }
  133.     XInstallColormap(display, colmap);
  134.     color_Blue = mycolor.pixel;
  135.  
  136.     if (XParseColor(display, colmap, "orange", &mycolor, &mycolor) < 1) {
  137.     fprintf(stderr, "Error parsing color %s:", "red");
  138.     perror((char *) NULL);
  139.     exit(0);
  140.     }
  141.     mycolor.flags = DoRed | DoGreen | DoBlue;
  142.     if (XAllocColor(display, colmap, &mycolor) < 1) {
  143.     fprintf(stderr, "Error allocating color ");
  144.     perror((char *) NULL);
  145.     }
  146.     XInstallColormap(display, colmap);
  147.     color_Orange = mycolor.pixel;
  148.  
  149.     if (XParseColor(display, colmap, "violet red", &mycolor, &mycolor) < 1) {
  150.     fprintf(stderr, "Error parsing color %s:", "red");
  151.     perror((char *) NULL);
  152.     exit(0);
  153.     }
  154.     mycolor.flags = DoRed | DoGreen | DoBlue;
  155.     if (XAllocColor(display, colmap, &mycolor) < 1) {
  156.     fprintf(stderr, "Error allocating color ");
  157.     perror((char *) NULL);
  158.     }
  159.     XInstallColormap(display, colmap);
  160.     color_vRed = mycolor.pixel;
  161.  
  162.     if (XParseColor(display, colmap, "medium aquamarine", &mycolor, &mycolor) < 1) {
  163.     fprintf(stderr, "Error parsing color %s:", "red");
  164.     perror((char *) NULL);
  165.     exit(0);
  166.     }
  167.     mycolor.flags = DoRed | DoGreen | DoBlue;
  168.     if (XAllocColor(display, colmap, &mycolor) < 1) {
  169.     fprintf(stderr, "Error allocating color ");
  170.     perror((char *) NULL);
  171.     }
  172.     XInstallColormap(display, colmap);
  173.     color_Green = mycolor.pixel;
  174.  
  175.     /* stuff needed for changing colors and other X-windows stuff */
  176.     xid = (XID) xv_get(xvwin, XV_XID);
  177.     gc_val.foreground = BlackPixel(display, DefaultScreen(display));
  178.     gc_val.background = WhitePixel(display, DefaultScreen(display));
  179.     gc_val.font = (Font) xv_get(font_small, XV_XID);
  180.     gc = XCreateGC(display, xid, GCForeground | GCBackground | GCFont, &gc_val);
  181. }
  182.  
  183.  
  184.  /************************************************************/
  185. void
  186. load_proc()
  187. {
  188.     char     *fname;
  189.  
  190.     fname = (char *) xv_get(hist_win->file_item, PANEL_VALUE);
  191.  
  192.     if (fname == NULL || strlen(fname) == 0)
  193.     return;
  194.  
  195. /*  printf("in load_proc, fname = %s\n", fname);  */
  196.  
  197.     if (cur_fname == NULL || strcmp(fname, cur_fname) != 0) {    /* only if new file name */
  198.  
  199.     /* free any previously allocated stuff */
  200.     if (size != 0) {
  201.         if (pix_format == PFINT)
  202.         cfree(i_hist);
  203.         else if (pix_format == PFSHORT)
  204.         cfree(s_hist);
  205.         else if (pix_format == PFFLOAT)
  206.         cfree(f_hist);
  207.  
  208.         size = 0;
  209.     }
  210.     if (cur_fname != NULL && strcmp(cur_fname, "<stdin>") != 0)
  211.         free(cur_fname);
  212.     cur_fname = (char *) malloc(100);
  213.     strcpy(cur_fname, fname);
  214.     if (load_image() < 0)
  215.         return;
  216.  
  217.     restore_proc();        /* draw the picture     */
  218.     }                /* end of if new file     */
  219. }                /* end of load_proc         */
  220.  
  221. set_count_proc()
  222. {
  223.     char     *s;
  224.     int       i;
  225.     int       n;
  226.  
  227.     s = (char *) xv_get(hist_win->count, PANEL_VALUE, NULL);
  228.  
  229.     i = 0;            /* index of s         */
  230.     while (s[i] == ' ' || s[i] == '\t')
  231.     i++;            /* skip beginning blanks  */
  232.  
  233.     if (s[i] < '0' || '9' < s[i])
  234.     return;
  235.  
  236.     n = 0;            /* sum     */
  237.     while ('0' <= s[i] && s[i] <= '9') {
  238.     n = n * 10 + (s[i] - '0');
  239.     i++;
  240.     }
  241.  
  242.     if (Bottom <= n && n <= Top)
  243.     set_NewBottom(n);
  244. }
  245.  
  246. check_proc()
  247. {                /* check specified pixel value     */
  248.     char     *s;
  249.     int       i, j;
  250.     int       pv_sign, n;
  251.     float     f, g;
  252.  
  253.     s = (char *) xv_get(hist_win->pixel_value, PANEL_VALUE, NULL);
  254.  
  255.     i = 0;            /* index of s         */
  256.     while (s[i] == ' ' || s[i] == '\t')
  257.     i++;            /* skip beginning blanks  */
  258.  
  259.     if (s[i] == '-') {
  260.     pv_sign = -1;
  261.     i++;
  262.     } else
  263.     pv_sign = 1;
  264.  
  265.     if (s[i] < '0' || '9' < s[i])
  266.     return;
  267.  
  268.     n = 0;            /* sum     */
  269.     while ('0' <= s[i] && s[i] <= '9') {
  270.     n = n * 10 + (s[i] - '0');
  271.     i++;
  272.     }
  273.  
  274.     n = n * pv_sign;
  275.  
  276.     if (pix_format != PFFLOAT) {
  277. #ifdef aax
  278.     printf("in check_proc, pixel value=%d\n", n);
  279. #endif
  280.  
  281.     for (j = 0; j <= range; j++)
  282.         if (histo_va[j] <= n && n < histo_va[j + 1]) {
  283.         mouse_move(j);
  284.         break;
  285.         }
  286.     return;
  287.     }
  288.     /* float     */
  289.     f = 0.;
  290.     if (s[i] == '.') {
  291.     i++;
  292.     if ('0' <= s[i] && s[i] <= '9') {
  293.         f = .1 * (s[i] - '0');
  294.         i++;
  295.         if ('0' <= s[i] && s[i] <= '9')
  296.         f = f + .01 * (s[i] - '0');
  297.     }
  298.     }
  299.     g = f * pv_sign + n;
  300.  
  301. #ifdef aax
  302.     printf("in check_proc, pixel value=%.2f\n", g);
  303. #endif
  304.  
  305.     for (j = 0; j <= range; j++)
  306.     if (fhisto_va[j] <= g && g < fhisto_va[j + 1]) {
  307.         mouse_move(j);
  308.         break;
  309.     }
  310. }                /* end of check_proc ()     */
  311.  
  312. restore_proc()
  313. {
  314.     paint = 0;            /* fisrt picture of this image     */
  315.  
  316.     eval_proc();
  317. }
  318.  
  319. eval_proc()
  320. {
  321.     CV_HT = (int) xv_get(hist_win->canvas, XV_HEIGHT);    /* canvas height */
  322.     HGT = CV_HT - 9 * TXTHT;
  323.     /* printf("in eval_proc,  CV_HT=%d\n", CV_HT);  */
  324.  
  325.     comp_hg();            /* compute histo_graph         */
  326.     histogram_repaint_proc();
  327. }
  328.  
  329. /*************************************************************/
  330. void
  331. quit_proc()
  332. {
  333.     xv_set(hist_win->win, FRAME_NO_CONFIRM, TRUE, NULL);
  334.     (void) xv_destroy_safe(hist_win->win);
  335.     /* this will also kill all child windows */
  336. }
  337.  
  338. /*************************************************************/
  339. void
  340. move_left_proc()
  341. {
  342.     if (move_j == -1)
  343.     mouse_move(range / 3);
  344.     else if (move_j > 0)
  345.     mouse_move(move_j - 1);
  346. }
  347.  
  348. void
  349. move_right_proc()
  350. {
  351.     if (move_j == -1)
  352.     mouse_move(range * 2 / 3);
  353.     else if (move_j < range)
  354.     mouse_move(move_j + 1);
  355. }
  356.  
  357. void
  358. select_proc()
  359. {
  360.     if (NewLeft != -1 && NewRight != -1)
  361.     return;
  362.  
  363.     if (move_j == -1)
  364.     move_j = range / 3;
  365.  
  366.     if (NewLeft == -1)
  367.     NewLeft = move_j;    /* relative     */
  368.     else if (NewLeft < move_j)
  369.     NewRight = move_j;
  370.     else if (move_j < NewLeft) {
  371.     NewRight = NewLeft;
  372.     NewLeft = move_j;
  373.     } else
  374.     return;            /* NewLeft != -1 && NewLeft == move_j     */
  375.  
  376.     draw_vertibar();        /* draw vertical bars     */
  377. }
  378.  
  379. set_NewBottom(occ)        /* set NewBottom or NewTop     */
  380.     int       occ;        /* occurrence, Bottom <= occ <= Top         */
  381. {
  382.     if (NewBottom == -1)
  383.     NewBottom = occ;
  384.     else if (NewBottom < occ)
  385.     NewTop = occ;
  386.     else if (occ < NewBottom) {
  387.     NewTop = NewBottom;
  388.     NewBottom = occ;
  389.     } else
  390.     return;
  391.  
  392. #ifdef aax
  393.     printf("in set_NewBottom, NewBottom=%d  NewTop=%d\n",
  394.        NewBottom, NewTop);
  395. #endif
  396.  
  397.     draw_horizbar();        /* draw horizontal bar     */
  398. }
  399.  
  400. draw_horizbar()
  401. {                /* draw horizontal bar     */
  402.     char      label[30];
  403.     int       ypos;
  404.     int       occ;        /* occurrence, Bottom <= occ <= Top         */
  405.  
  406.     if (NewBottom != -1) {
  407.     occ = NewBottom;
  408.     ypos = 3 * TXTHT + HGT - (occ - Bottom) * y_unit;
  409.     XSetForeground(display, gc, color_Green);
  410.     HorDashLine(2 * TXTWD, 2 * TXTWD + range + 3, ypos);
  411.     sprintf(label, "%d", occ);
  412.     XDrawString(display, xid, gc, (int) (2 * TXTWD + range + 5), ypos + 4,
  413.             label, strlen(label));
  414.     }
  415.     if (NewTop != -1) {
  416.     occ = NewTop;
  417.     ypos = 3 * TXTHT + HGT - (occ - Bottom) * y_unit;
  418.     XSetForeground(display, gc, color_Green);
  419.     HorDashLine(2 * TXTWD, 2 * TXTWD + range + 3, ypos);
  420.     sprintf(label, "%d", occ);
  421.     XDrawString(display, xid, gc, (int) (2 * TXTWD + range + 5), ypos + 4,
  422.             label, strlen(label));
  423.     }
  424. }
  425.  
  426. draw_vertibar()
  427. {
  428.     int       x, y1, y2;
  429.     char      label[30];
  430.     Font_string_dims dims;
  431.     int       w0;
  432.  
  433.     if (NewLeft != -1) {
  434.     x = 2 * TXTWD + NewLeft;
  435.     XSetForeground(display, gc, color_Blue);
  436.     sprintf(label, "%d", histo_graph[NewLeft]);
  437.     xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  438.     w0 = dims.width / 2;
  439.     XDrawString(display, xid, gc, x - w0, 2 * TXTHT - 6,
  440.             label, strlen(label));
  441.  
  442.     XSetForeground(display, gc, color_Orange);
  443.     y1 = 2 * TXTHT;
  444.     y2 = 6 * TXTHT + HGT;
  445.     VerDashLine(x, y1, y2);
  446.     draw_pv(NewLeft, x, (int) (7 * TXTHT + HGT - 6));
  447.     }
  448.     if (NewRight != -1) {
  449.     x = 2 * TXTWD + NewRight;
  450.     XSetForeground(display, gc, color_Blue);
  451.     sprintf(label, "%d", histo_graph[NewRight]);
  452.     xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  453.     w0 = dims.width / 2;
  454.     XDrawString(display, xid, gc, x - w0, 2 * TXTHT - 6,
  455.             label, strlen(label));
  456.  
  457.     XSetForeground(display, gc, color_Orange);
  458.     y1 = 2 * TXTHT;
  459.     y2 = 6 * TXTHT + HGT;
  460.     VerDashLine(x, y1, y2);
  461.     draw_pv(NewRight, x, (int) (7 * TXTHT + HGT - 6));
  462.     }
  463. }                /* end of  draw_vertibar ()       */
  464.  
  465. draw_pv(j, x, y)
  466.     int       j, x, y;
  467. {
  468.     char      label[30];
  469.     Font_string_dims dims;
  470.     int       w0;
  471.  
  472.     if (pix_format != PFFLOAT) {
  473.     if (histo_va[j + 1] - histo_va[j] <= 1) {
  474.         sprintf(label, "%d", histo_va[j]);
  475.         xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  476.         w0 = dims.width / 2;
  477.         XDrawString(display, xid, gc, x - w0, y, label, strlen(label));
  478.     } else {
  479.         sprintf(label, "%d-", histo_va[j]);
  480.         xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  481.         w0 = dims.width / 2;
  482.         XDrawString(display, xid, gc, x - w0, y, label, strlen(label));
  483.         sprintf(label, "%d", histo_va[j + 1] - 1);
  484.         xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  485.         w0 = dims.width / 2;
  486.         XDrawString(display, xid, gc, x - w0, y + TXTHT, label, strlen(label));
  487.     }
  488.     } else {            /* float     */
  489.     sprintf(label, "%.2f-", fhisto_va[j]);
  490.     xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  491.     w0 = dims.width / 2;
  492.     XDrawString(display, xid, gc, x - w0, y, label, strlen(label));
  493.     sprintf(label, "%.2f", fhisto_va[j + 1] - .01);
  494.     xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  495.     w0 = dims.width / 2;
  496.     XDrawString(display, xid, gc, x - w0, y + TXTHT, label, strlen(label));
  497.     }
  498.  
  499. }                /* end of draw_pv     */
  500.  
  501. /******************************************************************/
  502.  
  503. /*
  504.  * Event callback function for `canvas'.
  505.  */
  506. Notify_value
  507. canvas_event_proc(win, event, arg, type)
  508.     Xv_Window win;
  509.     Event    *event;
  510.     Notify_arg arg;
  511.     Notify_event_type type;
  512. {
  513.     int       xpos, ypos;
  514.  
  515.     int       occ;        /* occurrence, Bottom <= occ <= Top         */
  516.  
  517.  
  518.     if (event_is_up(event))    /* ingore all up events */
  519.     return notify_next_event_func(win, event, arg, type);
  520.  
  521.     if (event_meta_is_down(event))    /* ingore all meta events */
  522.     return notify_next_event_func(win, event, arg, type);
  523.  
  524.     xpos = event_x(event);
  525.     ypos = event_y(event);
  526.  
  527.     if (size == 0 || xpos < 2 * TXTWD || xpos > 2 * TXTWD + range
  528.     || ypos < 3 * TXTHT || ypos > 3 * TXTHT + HGT)
  529.     return;
  530.  
  531.     XSetFunction(display, gc, GXcopy);
  532.     XSetForeground(display, gc, color_Red);
  533.  
  534.     switch (event_id(event)) {
  535.     case MS_LEFT:
  536.     case LOC_DRAG:
  537. /*    printf("in canvas_proc, LOC_MOVE,  xpos=%d  ypos=%d  size=%d\n",
  538.         xpos, ypos, size);  */
  539.     if (xpos - 2 * TXTWD != move_j)
  540.         mouse_move(xpos - 2 * TXTWD);
  541.     break;
  542.     case MS_MIDDLE:
  543.     if (xpos - 2 * TXTWD != move_j)
  544.         mouse_move(xpos - 2 * TXTWD);
  545.     select_proc();        /* select vertical bar   */
  546.     break;
  547.     case MS_RIGHT:
  548.     if (NewTop == -1 || NewBottom == -1) {
  549.         occ = Bottom + (3 * TXTHT + HGT - ypos) / y_unit;
  550.         set_NewBottom(occ);
  551.     }
  552.     break;
  553.     }
  554.  
  555.     XSetForeground(display, gc, BlackPixel(display, DefaultScreen(display)));
  556.     return notify_next_event_func(win, event, arg, type);
  557. }
  558.  
  559. /************************************************************************/
  560. /*
  561.  * Repaint callback function for `canvas'.
  562.  */
  563.  
  564. histogram_repaint_proc()
  565. {
  566.     /*
  567.      * this routine uses a combination of standard X and Sun pixwin calls.
  568.      * Probably should change everything to standard X, but I like of pw text
  569.      * routines better.
  570.      */
  571.     int       i;
  572.  
  573.     int       wd, ht;        /* frame width, height     */
  574.  
  575.     int       ypos;
  576.  
  577.     int       x, y1, y2;
  578.  
  579.     wd = (int) xv_get(hist_win->win, XV_WIDTH);
  580.     FRM_WD = range + 17 * TXTWD;
  581.     if (FRM_WD < 500)
  582.     FRM_WD = 500;
  583. /*    printf("in repaint, wd=%d   FRM_WD=%d\n", wd, FRM_WD); */
  584.     xv_set(hist_win->win, XV_WIDTH, FRM_WD, NULL);
  585.  
  586.     ht = (int) xv_get(hist_win->win, XV_HEIGHT);
  587.     CV_HT = (int) xv_get(hist_win->canvas, XV_HEIGHT);    /* canvas height */
  588.     HGT = CV_HT - 9 * TXTHT;
  589.     /* printf("in repaint, ht=%d   CV_HT=%d\n", ht, CV_HT); */
  590.     XMapWindow(dpy, hist_win->win);
  591.  
  592.     XSetFunction(display, gc, GXclear);
  593.     XSetForeground(display, gc, BlackPixel(display, DefaultScreen(display)));
  594.     XSetBackground(display, gc, WhitePixel(display, DefaultScreen(display)));
  595.     /* XClearWindow(display, cpw); */
  596.  
  597.     xv_rop(cpw, 0, 0, (int) xv_get(cpw, XV_WIDTH, NULL),
  598.        (int) xv_get(cpw, XV_HEIGHT, 0),
  599.        PIX_CLR, NULL, 0, 0);
  600.  
  601.     XSetFunction(display, gc, GXcopy);
  602.  
  603.     if (size == 0)
  604.     return;            /* no image         */
  605.  
  606.     pri_hint();            /* print hint         */
  607. /*    printf("in repaint, after pri_hint(), cv_wd=%d  cv_ht=%d\n",
  608.         xv_get(cpw, XV_WIDTH), xv_get(cpw, XV_HEIGHT) );  */
  609.  
  610.     XSetForeground(display, gc, color_Black);    /* black     */
  611.     y_unit = HGT / (Top - Bottom);
  612.     for (i = 0; i <= range; i++) {
  613.     if (histo_graph[i] > 0) {
  614.         x = 2 * TXTWD + i;
  615.         y1 = 3 * TXTHT + HGT;
  616.         y2 = 3 * TXTHT + HGT - (histo_graph[i] - Bottom) * y_unit;
  617.  
  618.         XDrawLine(display, xid, gc, x, y1, x, y2);
  619.     }
  620.     }
  621.  
  622.     draw_coor();        /* draw coordinates     */
  623.  
  624.     XMapWindow(dpy, hist_win->win);
  625.  
  626.     move_j = -1;
  627.     NewLeft = -1;
  628.     NewRight = -1;
  629.     NewTop = -1;
  630.     NewBottom = -1;
  631. }
  632.  
  633. draw_coor()
  634. {                /* draw coordinates  */
  635.     int       i;
  636.     int       xpos;
  637.     int       ypos;
  638.  
  639.     /* Horizontal labels, vertical lines */
  640.     XSetForeground(display, gc, color_Blue);    /* blue     */
  641.  
  642.     for (i = 0; i < end_x; i++) {
  643.     xpos = x_pos[i];
  644.     VerDashLine(xpos, 3 * TXTHT, (int) (3 * TXTHT + HGT + 5));
  645.     }
  646.  
  647.     ypos = 3 * TXTHT;
  648.     XDrawLine(display, xid, gc, 2 * TXTWD, ypos, 2 * TXTWD + range + 3, ypos);
  649.     ypos = 3 * TXTHT + HGT;
  650.     XDrawLine(display, xid, gc, 2 * TXTWD, ypos, 2 * TXTWD + range + 3, ypos);
  651.     ypos = 3 * TXTHT + HGT + 1;
  652.     XDrawLine(display, xid, gc, 2 * TXTWD, ypos, 2 * TXTWD + range, ypos);
  653.  
  654.     for (i = 0; i < end_y; i++) {
  655.     ypos = y_pos[i];
  656.     HorDashLine(2 * TXTWD, (int) (2 * TXTWD + range + 5), ypos);
  657.     }
  658.     xy_coor();            /* 0  50  100  150  200     */
  659. }
  660.  
  661. xy_coor()
  662. {
  663.  
  664.     int       i, j;
  665.     int       xpos, x0, x1;
  666.  
  667.     XSetForeground(display, gc, color_vRed);
  668.  
  669.     end_xc = 0;            /* end of x_cover_l[] and x_cover_r[]     */
  670.  
  671.     one_x_lab(0);
  672.     one_x_lab(end_x - 1);
  673.     for (i = 1; i < end_x - 1; i++)
  674.     one_x_lab(i);
  675.  
  676.     XSetForeground(display, gc, color_Blue);
  677.  
  678.     end_yc = 0;            /* end of y_cover_s[] and y_cover_g[]     */
  679.  
  680.     one_y_lab(0);
  681.     one_y_lab(end_y - 1);
  682.     for (i = 1; i < end_y - 1; i++)
  683.     one_y_lab(i);
  684.  
  685. }                /* end of xy_coor         */
  686.  
  687. one_x_lab(i)            /* print one x label     */
  688.     int       i;
  689. {
  690.     char      label[30];
  691.     Font_string_dims dims;
  692.     int       w0;
  693.  
  694.     int       j;
  695.     int       xpos, x0, x1;
  696.  
  697.     xpos = x_pos[i];
  698.     if (pix_format != PFFLOAT)
  699.     sprintf(label, "%d", x_label[i]);
  700.     else
  701.     sprintf(label, "%.2f", fx_label[i]);
  702.  
  703.     xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  704.     /*
  705.      * printf("in repaint.., string-width=%d  string-height=%d", dims.width,
  706.      * dims.height );
  707.      */
  708.     w0 = dims.width / 2;
  709.     x0 = xpos - w0;
  710.     x1 = xpos + w0;
  711.     for (j = 0; j < end_xc; j++)
  712.     if ((x_cover_l[j] <= x0 && x0 <= x_cover_r[j]) ||
  713.         (x_cover_l[j] <= x1 && x1 <= x_cover_r[j]))
  714.         break;
  715.  
  716.     if (j < end_xc)
  717.     return;
  718.  
  719.     x_cover_l[end_xc] = x0;
  720.     x_cover_r[end_xc] = x1;
  721.     end_xc++;
  722.     XDrawString(display, xid, gc, x0, (int) (4 * TXTHT + HGT - 1),
  723.         label, strlen(label));
  724. }
  725.  
  726. one_y_lab(i)            /* print one y label     */
  727.     int       i;
  728. {
  729.     char      label[30];
  730.     Font_string_dims dims;
  731.     int       h0;
  732.  
  733.     int       j;
  734.     int       ypos, y0, y1;
  735.  
  736.     ypos = y_pos[i];
  737.  
  738.     sprintf(label, "%d", y_label[i]);
  739.     xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  740.  
  741.     /*
  742.      * printf("in repaint.., string-width=%d  string-height=%d", dims.width,
  743.      * dims.height );
  744.      */
  745.  
  746.     h0 = dims.height / 2;
  747.     y0 = ypos - h0;
  748.     y1 = ypos + h0;
  749.     for (j = 0; j < end_yc; j++)
  750.     if ((y_cover_s[j] <= y0 && y0 <= y_cover_g[j]) ||
  751.         (y_cover_s[j] <= y1 && y1 <= y_cover_g[j]))
  752.         break;
  753.  
  754.     if (j < end_yc)
  755.     return;
  756.  
  757.     y_cover_s[end_yc] = y0;
  758.     y_cover_g[end_yc] = y1;
  759.     end_yc++;
  760.     XDrawString(display, xid, gc, 2 * TXTWD + range + 5, ypos + 4,
  761.         label, strlen(label));
  762. }
  763.  
  764. VerDashLine(x, y1, y2)
  765.     int       x, y1, y2;
  766. {
  767.     int       m;
  768.     int       i;
  769.  
  770.     XPoint   *q;
  771.  
  772.     m = (y2 - y1) / 2;
  773.  
  774.     q = Calloc(m, XPoint);
  775.  
  776.     q[0].x = x;
  777.     q[0].y = y1;
  778.     for (i = 1; i < m; i++) {
  779.     q[i].x = 0;
  780.     q[i].y = 2;
  781.     }
  782.  
  783.     XDrawPoints(display, xid, gc, q, m, CoordModePrevious);
  784. }
  785.  
  786. HorDashLine(x1, x2, y)
  787.     int       x1, x2, y;
  788. {
  789.     int       m;
  790.     int       i;
  791.  
  792.     XPoint   *q;
  793.  
  794.     m = (x2 - x1) / 2;
  795.  
  796.     q = Calloc(m, XPoint);
  797.  
  798.     q[0].x = x1;
  799.     q[0].y = y;
  800.     for (i = 1; i < m; i++) {
  801.     q[i].x = 2;
  802.     q[i].y = 0;
  803.     }
  804.  
  805.     XDrawPoints(display, xid, gc, q, m, CoordModePrevious);
  806. }
  807.  
  808. mouse_move(j)
  809.     int       j;
  810. {
  811.     char      label[30];
  812.     Font_string_dims dims;
  813.     int       w0;
  814.     int       x;
  815.  
  816.     if (move_j >= 0)
  817.     move_rcv();
  818.     move_j = j;
  819.  
  820.     x = 2 * TXTWD + move_j;
  821.     XSetForeground(display, gc, color_Blue);    /* blue     */
  822.     sprintf(label, "%d", histo_graph[j]);
  823.     xv_get(font_small, FONT_STRING_DIMS, label, &dims);
  824.     w0 = dims.width / 2;
  825.     XDrawString(display, xid, gc, x - w0, 3 * TXTHT - 6, label, strlen(label));
  826.  
  827.     XSetForeground(display, gc, color_Red);    /* red     */
  828.     VerDashLine(x, 3 * TXTHT, (int) (4 * TXTHT + HGT));
  829.  
  830.     draw_pv(j, x, (int) (5 * TXTHT + HGT - 6));
  831.  
  832. }                /* end of mouse_move         */
  833.  
  834. move_rcv()
  835. {
  836.     int       x, y1, y2;
  837.     int       y;
  838.  
  839.     x = 2 * TXTWD + move_j;
  840.     XSetForeground(display, gc, color_White);
  841.     XDrawLine(display, xid, gc, x, 3 * TXTHT, x, (int) (4 * TXTHT + HGT));
  842.     for (y = 4 * TXTHT + HGT; y <= 6 * TXTHT + HGT; y++)
  843.     XDrawLine(display, xid, gc, 0, y, (int) (5 * TXTWD + range - 1), y);
  844.     for (y = 2 * TXTHT; y < 3 * TXTHT; y++)
  845.     XDrawLine(display, xid, gc, 0, y, (int) (5 * TXTWD + range), y);
  846.  
  847.     if (histo_graph[move_j] > 0) {
  848.     XSetForeground(display, gc, color_Black);
  849.     y1 = 3 * TXTHT + HGT;
  850.     y2 = 3 * TXTHT + HGT - (histo_graph[move_j] - Bottom) * y_unit;
  851.     XDrawLine(display, xid, gc, x, y1, x, y2);
  852.     }
  853.     draw_coor();        /* draw coordinates     */
  854.     draw_vertibar();        /* draw vertical bars     */
  855.     draw_horizbar();        /* draw horizontal bar     */
  856. }
  857.  
  858. pri_hint()
  859. {                /* print hint     */
  860.     int       i;
  861.     int       x, y;
  862.     XGCValues gc_val;
  863.     char      label[40];
  864.     int       b;        /* base index of hint_s[]     */
  865.  
  866. /*
  867.  for (i=0; i<7; i++)
  868.    printf("%s  str_len=%d\n", hint_s[i], strlen(hint_s[i]) );
  869. */
  870.  
  871.     gc_val.font = (Font) xv_get(font_bold, XV_XID);
  872.     gc = XCreateGC(display, xid, GCForeground | GCFont, &gc_val);
  873.  
  874.     XSetForeground(display, gc, color_Blue);    /* blue     */
  875.     XDrawString(display, xid, gc, TXTWD, TXTHT - 6, "Count", 5);
  876.  
  877.     XSetForeground(display, gc, color_Red);
  878.     XDrawString(display, xid, gc, TXTWD, CV_HT - 7, "pixel value", 11);
  879.  
  880.     XSetForeground(display, gc, color_Black);    /* black     */
  881.  
  882.     b = 13;
  883.     sprintf(hint_s[b], "On the current graph -------");
  884.  
  885.     if (pix_format == PFFLOAT)
  886.     sprintf(hint_s[b + 1], " Left pixel value = %.2f", fhisto_va[0]);
  887.     else
  888.     sprintf(hint_s[b + 1], " Left pixel value = %d", histo_va[0]);
  889.  
  890.     if (pix_format == PFFLOAT)
  891.     sprintf(hint_s[b + 2], " Right pixel value = %.2f",
  892.         fhisto_va[range + 1] - .01);
  893.     else
  894.     sprintf(hint_s[b + 2], " Right pixel value = %d",
  895.         histo_va[range + 1] - 1);
  896.  
  897.     sprintf(hint_s[b + 3], " min count = %d", Bottom);
  898.     sprintf(hint_s[b + 4], " Max count = %d", Top);
  899.  
  900.     sprintf(hint_s[b + 5], "");
  901.  
  902.     sprintf(hint_s[b + 6], " # of different pixel values :");
  903.     sprintf(hint_s[b + 7], "    %d", cur_nofdv);
  904.  
  905.     sprintf(hint_s[b + 8], "");
  906.  
  907.     sprintf(hint_s[b + 9], " # of pixels : %d", cur_size);
  908.     /* # of pixels in current graph */
  909.     sprintf(hint_s[b + 10], " distribution : %.2f %%",
  910.         (float) cur_size * 100. / (float) size);
  911.  
  912.     x = 5 * TXTWD + range;
  913.     for (i = 0; i <= b + 10; i++) {
  914.     y = i * TXTHT + 12;
  915.     XDrawString(display, xid, gc, x, y, hint_s[i], strlen(hint_s[i]));
  916.     }
  917.  
  918.     gc_val.font = (Font) xv_get(font_small, XV_XID);
  919.     gc = XCreateGC(display, xid, GCForeground | GCBackground, &gc_val);
  920.  
  921.     XDrawLine(display, xid, gc, x, b * TXTHT + 16, x, CV_HT - 8);
  922.  
  923. }                /* end of pri_hint     */
  924.